﻿var roles = ["Admin", "Manager", "Developer", "Tester"];
var editState = false, added = false;
var itemsToRemove = "";

$(document).ready(function () {

    $("#closeAddUserPop").click(function() {
        $('#overlay, #addUserPop').fadeOut(1000);
        setTimeout(function() {
            $('#overlay').css({ 'height': '0' });
            clearFields();
        }, 1000);
    });
});

function clearFields() {
    $('#ContentPlaceHolder2_nameBox, #ContentPlaceHolder2_snameBox, ' +
                '#ContentPlaceHolder2_patronymicBox, #ContentPlaceHolder2_loginBox, ' +
                '#ContentPlaceHolder2_passwordBox, #ContentPlaceHolder2_cpasswordBox, ' +
                '#ContentPlaceHolder2_emailBox').val("").css({ 'background': 'url("/Resources/img/Admin/input.png") no-repeat' });
}

function doPanelPostBack(eventTarget, eventArgument) {
    $('#ContentPlaceHolder1___EVENTTARGETCUR').val(eventTarget);
    $('#ContentPlaceHolder1___EVENTARGUMENTCUR').val(eventArgument);
    //document.getElementById('ContentPlaceHolder1_updatePanel1').submit();
}

function addUserBtnClick() {
    $('#overlay').css({ 'height': $(document).height() + 'px' });
    clearFields();
    $('#overlay, #addUserPop').fadeIn(1000);
}

function addOptionsToSelecs() {
    $('.adminRole').each(function () {
        var role = this.textContent;
        $(this).find('option[value="1"]').remove();
        for (var i = 0; i < roles.length; i++) {
            
            var option = document.createElement("option");
            option.text = roles[i];
            if (roles[i] == role)
                option.selected = "selected";
            this.add(option);
        }
    });
}

function removeBtnClick(thisDiv, id) {
    if (itemsToRemove.indexOf(id) >= 0) {
        $(thisDiv).parent().parent().css({ 'background': '' });
        itemsToRemove=itemsToRemove.replace(id + ",", "");
    } else {
        $(thisDiv).parent().parent().css({ 'background': '#FFB2B2' });
        itemsToRemove += id + ",";
    }
}

function editAdmin() {
    itemsToRemove = "";
    editState = true;
    $('.adminName, .adminSname, .adminPatron, .adminLogin, .adminEmail, .adminRole').css({ 'background': 'white', 'border': '2px inset' });
    $('.adminRole').css({ '-webkit-appearance': 'menulist' }).removeAttr("disabled");
    $('.adminName, .adminSname, .adminPatron, .adminLogin, .adminEmail').removeAttr("readonly");
    $('.editBtn').text("Save");
    $('.rm').show();
    if (!added) {
        addOptionsToSelecs();
        added = true;
    }
    return false;
}

function validateInput() {
    editState = false;
    added = false;
    var itemsToEdit = "";
    for (var i = 1; i <= itemsCount; i++) {
        if (itemsToRemove.indexOf($("#adminId" + i).text() + ",") < 0) {
            itemsToEdit += $("#adminId" + i).text() + "+";
            itemsToEdit += $("#adminName" + i).val() + "+";
            itemsToEdit += $("#adminSname" + i).val() + "+";
            itemsToEdit += $("#adminPatron" + i).val() + "+";
            itemsToEdit += $("#adminEmail" + i).val() + "+";
            itemsToEdit += $("#adminLogin" + i).val() + "+";
            itemsToEdit += $("#adminRole" + i).val() + "$";
        }
    }
    doPanelPostBack("editUser", itemsToRemove + "~" + itemsToEdit);
    return true;
}

function validateUserAdd() {
    var name = $('#ContentPlaceHolder2_nameBox');
    var sname = $('#ContentPlaceHolder2_snameBox');
    var patronymic = $('#ContentPlaceHolder2_patronymicBox');
    var login = $('#ContentPlaceHolder2_loginBox');
    var password = $('#ContentPlaceHolder2_passwordBox');
    var cpassword = $('#ContentPlaceHolder2_cpasswordBox');
    var email = $('#ContentPlaceHolder2_emailBox');
    var flag = true;
    if (!validateName(name, sname, patronymic))
        flag = false;
    if (!validateEmail(email))
        flag = false;
    if (!validateName(login, password, cpassword))
        flag = false;
    if (!passwordsCorrespond(password, cpassword)) {
        flag = false;
    }
    if (flag)
        $('#ContentPlaceHolder1___EVENTTARGETCUR').val("");
    return flag;
}

function passwordsCorrespond(pass, cpass) {
    if (pass.val() != cpass.val()) {
        pass.css({ 'background': 'url("/Resources/img/Admin/inputErr.png") no-repeat' });
        cpass.css({ 'background': 'url("/Resources/img/Admin/inputErr.png") no-repeat' });
        alert("Passwords do not correspond to each other!!!");
        return false;
    }
    return true;
}

function validateName(selector1, selector2, selector3) {
    var flag = true;
    var regex = new RegExp("^ +$");
    if (selector1.val() == "" || regex.test(selector1.val())) {
        flag = false;
        selector1.css({ 'background': 'url("/Resources/img/Admin/inputErr.png") no-repeat' });
    } else {
        selector1.css({ 'background': 'url("/Resources/img/Admin/input.png") no-repeat' });
    }
    if (selector2.val() == "" || regex.test(selector2.val())) {
        flag = false;
        selector2.css({ 'background': 'url("/Resources/img/Admin/inputErr.png") no-repeat' });
    } else {
        selector2.css({ 'background': 'url("/Resources/img/Admin/input.png") no-repeat' });
    }
    if (selector3.val() == "" || regex.test(selector3.val())) {
        flag = false;
        selector3.css({ 'background': 'url("/Resources/img/Admin/inputErr.png") no-repeat' });
    } else {
        selector3.css({ 'background': 'url("/Resources/img/Admin/input.png") no-repeat' });
    }
    return flag;
}

function validateEmail(selector) {
    var regex = new RegExp("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$");
    if (regex.test(selector.val()) && selector.val() != "") {
        selector.css({ 'background': 'url("/Resources/img/Admin/input.png") no-repeat' });
        return true;
    } else {
        selector.css({ 'background': 'url("/Resources/img/Admin/inputErr.png") no-repeat' });
        return false;
    }
}